const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
  const Reservation = sequelize.define('Reservation', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    user_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '用户ID'
    },
    room_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '房间ID'
    },
    date: {
      type: DataTypes.DATEONLY,
      allowNull: false,
      comment: '预约日期'
    },
    start_time: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '开始时间'
    },
    end_time: {
      type: DataTypes.STRING,
      allowNull: false,
      comment: '结束时间'
    },
    duration: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '时长(小时)'
    },
    is_next_day: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      comment: '是否次日结束'
    },
    amount: {
      type: DataTypes.DECIMAL(10, 2),
      allowNull: false,
      comment: '金额'
    },
    status: {
      type: DataTypes.ENUM('pending', 'confirmed', 'useing', 'canceled', 'completed'),
      defaultValue: 'pending',
      comment: '状态：pending-待确认，confirmed-已确认待使用，useing-使用中，canceled-已取消，completed-已完成'
    }
  }, {
    tableName: 'reservations',
    timestamps: true,
    createdAt: 'create_time',
    updatedAt: 'update_time'
  });

  // 添加关联关系
  Reservation.associate = (models) => {
    // 预约与用户的关系
    Reservation.belongsTo(models.User, {
      foreignKey: 'user_id',
      as: 'user'
    });

    // 预约与房间的关系
    Reservation.belongsTo(models.Room, {
      foreignKey: 'room_id',
      as: 'room'
    });

    // 预约与订单的关系
    Reservation.hasOne(models.Order, {
      foreignKey: 'reservation_id',
      as: 'order'
    });
  };

  return Reservation;
}; 